﻿Imports System.IO

Public Class DfHelper

    Public Const version As Integer = 2
    Public Const validIdChars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZamcdefghijklmnopqrstuvwxyz1234567890"

    Private Shared mReletiveBase As String = Nothing
    Public Shared Sub setReletivePathBase(baseDirectory As String)
        mReletiveBase = baseDirectory
    End Sub

    Public Shared Function MakeRelativePath(toPath As String) As String
        Return MakeRelativePath(mReletiveBase, toPath)
    End Function

    ''' <summary>
    ''' Creates a relative path from one file or folder to another.
    ''' </summary>
    ''' <param name="toPath">Contains the path that defines the endpoint of the relative path.</param>
    ''' <returns>The relative path from the start directory to the end path.</returns>
    ''' <exception cref="ArgumentNullException"></exception>
    Public Shared Function MakeRelativePath(fromPath As String, toPath As String) As String
        If Not fromPath.EndsWith("\") Then
            fromPath &= "\"
        End If
        Dim fromUri As New Uri(fromPath)
        Dim toUri As New Uri(toPath)

        Dim relativeUri As Uri = fromUri.MakeRelativeUri(toUri)
        Dim relativePath As String = Uri.UnescapeDataString(relativeUri.ToString())

        Return relativePath.Replace("/"c, Path.DirectorySeparatorChar)
    End Function

    Public Shared Function MakeReletivePathFromFile(fromPath As String, toPath As String) As String
        Return MakeRelativePath(Path.GetDirectoryName(fromPath), toPath)
    End Function

    Public Shared Function GetCharCodeFromAscii(ascii As Integer) As String
        Dim ch As Char = Convert.ToChar(ascii)
        If Char.IsControl(ch) OrElse ascii > 127 Then
            Return ascii.ToString
        Else
            Return "'" & ch & "'"
        End If
    End Function

    Public Shared Function GetAsciiFromCharCode(charCode As String) As Integer
        Dim val As Integer = -1
        If Integer.TryParse(charCode, val) Then
            Return val
        Else
            If charCode.Length = 3 AndAlso charCode.StartsWith("'") AndAlso charCode.EndsWith("'") Then
                Return Convert.ToInt32(charCode(1))
            End If
        End If
        Throw New ArgumentException("Improperly formatted charCode: " & charCode)
    End Function

    Public Shared Sub DeleteDirectory(path As String)
        Dim files As String() = Directory.GetFiles(path)
        Dim dirs As String() = Directory.GetDirectories(path)

        For Each File As String In files
            IO.File.SetAttributes(File, FileAttributes.Normal)
            IO.File.Delete(File)
        Next

        For Each dir As String In dirs
            DeleteDirectory(dir)
        Next

        Directory.Delete(path, False)
    End Sub

    Public Shared Function HasInvalidIdChars(id As String) As Boolean
        For Each c As Char In id
            If Not validIdChars.Contains(c) Then
                Return True
            End If
        Next
        Return False
    End Function

    Public Shared Function ReadTilesets() As List(Of tileSetData)
        Dim ret As New List(Of tileSetData)
        Try
            For Each file As String In IO.Directory.GetFiles("Data\Tilesets")
                Dim extension As String = Path.GetExtension(file).ToLower
                If extension = ".png" OrElse extension = ".bmp" Then
                    Dim entry As New tileSetData
                    entry.Path = file
                    Dim name As String = IO.Path.GetFileName(file)
                    entry.Name = name
                    entry.Size = "unknown"
                    Try
                        Dim image As Image = image.FromFile(file)
                        Dim w As Integer = image.Width \ 16
                        Dim h As Integer = image.Height \ 16
                        entry.Size = String.Format("{0}x{1}", w, h)
                        image.Dispose()
                    Catch ex As Exception
                        'skip the size
                    End Try

                    ret.Add(entry)
                End If
            Next
        Catch ex As Exception

        End Try
        Return ret
    End Function

    Public Shared Function ReadColorSets() As List(Of String)
        Dim colorSets As New List(Of String)
        Try
            For Each file As String In IO.Directory.GetFiles("Data\Colors", "*.txt")
                Dim name As String = Path.GetFileName(file)
                colorSets.Add(name)
            Next
        Catch ex As Exception
        End Try
        Return colorSets
    End Function

End Class
